package com.ruoyi.common.core.domain.model

import com.alibaba.fastjson2.annotation.JSONField
import com.ruoyi.common.core.domain.entity.SysUser
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

/**
 * 登录用户身份权限
 *
 * @author ruoyi
 */
class LoginUser : UserDetails {
    /**
     * 用户ID
     */
    @JvmField
    var userId: Long? = null

    /**
     * 部门ID
     */
    var deptId: Long? = null

    /**
     * 用户唯一标识
     */
    @JvmField
    var token: String? = null

    /**
     * 登录时间
     */
    @JvmField
    var loginTime: Long? = null

    /**
     * 过期时间
     */
    @JvmField
    var expireTime: Long? = null

    /**
     * 登录IP地址
     */
    @JvmField
    var ipaddr: String? = null

    /**
     * 登录地点
     */
    @JvmField
    var loginLocation: String? = null

    /**
     * 浏览器类型
     */
    @JvmField
    var browser: String? = null

    /**
     * 操作系统
     */
    @JvmField
    var os: String? = null

    /**
     * 权限列表
     */
    @JvmField
    var permissions: Set<String>? = null

    /**
     * 用户信息
     */
    @JvmField
    var user: SysUser? = null

    constructor()
    constructor(user: SysUser?, permissions: Set<String>?) {
        this.user = user
        this.permissions = permissions
    }

    constructor(userId: Long?, deptId: Long?, user: SysUser?, permissions: Set<String>?) {
        this.userId = userId
        this.deptId = deptId
        this.user = user
        this.permissions = permissions
    }

    @JSONField(serialize = false)
    override fun getPassword(): String {
        return user?.password!!
    }

    override fun getUsername(): String {
        return user?.userName!!
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    override fun isAccountNonExpired(): Boolean {
        return true
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    override fun isAccountNonLocked(): Boolean {
        return true
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return
     */
    @JSONField(serialize = false)
    override fun isCredentialsNonExpired(): Boolean {
        return true
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    override fun isEnabled(): Boolean {
        return true
    }

    override fun getAuthorities(): Collection<GrantedAuthority?> {
        return emptyList()
    }

    companion object {
        private const val serialVersionUID = 1L
    }
}
